<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <title>主页面</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
    <link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
    <script src="//apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
    <script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.13.42/dingtalk.open.js"></script>

</head>
<script type="text/javascript">

    $(function (){
        var arrayObj = new Array();

        $("#checkresult").hide();
        $("#checknotfind").hide();

        //先给名字输入框赋值，方便联想查找。因为钉钉里面名字也是唯一的所以不用担心重名。
        $.ajax({
            url:"database_message",
            dataType:"json",
            type:"post",
            success:function(jsonArray){
                var length=jsonArray.length;
                for (var i=0;i<length;i++){
                    arrayObj.push(jsonArray[i].uname)
                }
                //这两个是为了填充input实现联想查找
                $("#user").autocomplete({
                    source: jsonArray,
                    // 定义至少输入几个字
                    minLength: 1,
                    max:10,
                    scroll:true,
                    scrollHeight: 300
                });
                $("#user_").autocomplete({
                    source: jsonArray,
                    minLength: 1,
                    max:10,
                    scroll:true,
                    scrollHeight: 300
                });
            },
            error:function(){
                alert("数据库读取名字失败！")
            }
        })

        //点击check开始扫码
        $("#check").click(function (){
            dd.ready(function() {
                dd.biz.util.scan({
                    type: "all" , // type 为 all、qrCode、barCode，默认是all。
                    onSuccess: function(data) {
                        var message = data.text;
                        alert(message)
                        find(message)
                    },
                    onFail : function(err) {

                    }
                });
            });
        })

        //盘点表没有该资产的时候
        $("#submit_not_true").click(function (){
            var rid = document.getElementById("rid_").innerText;
            var uname = document.getElementById("user_").value;
            $.ajax({
                url:"noResourceError",
                type:"post",
                dataType:"text",
                data:{
                    "rid":rid,
                    "uname":uname,
                },
                success:function (msg){
                    alert(msg)
                },
                error:function (msg){
                    alert("结果未提交！请检查输入的名字是否正确！\n"+"（以钉钉內的名字为准）")
                }
            })
        })

        //当盘点表有该资产的时候，点击提交按钮，right为0则是假，获取输入的uname传输到后端进行修正。right为1则是真，直接该盘点一致即可。
        $("#submit").click(function (){
            var right = 0;
            var checkid = document.getElementById("checkid").innerText;
            var rid = document.getElementById("rid").innerText;
            var uname = document.getElementById("user").value;
            var input = document.getElementsByTagName("input")
            var length = input.length
            for (var i=0;i<length;i++) {
                var obj = input[i];
                if (obj.type == "checkbox" && obj.checked) {
                    right = 1;
                }
            }
            alert("rid:"+rid+"  checkid:"+checkid)
            $.ajax({
                url:"checkResult",
                type:"post",
                dataType:"text",
                data:{
                    "right":right,
                    "checkid":checkid,
                    "rid":rid,
                    "uname":uname,
                },
                success:function (msg){
                    alert(msg)
                },
                error:function (){
                    alert("结果未提交！请检查输入的名字是否正确！\n"+"（以钉钉內的名字为准）")
                }
            })
        })
    });

    //先查找到对应的盘点单号（防止多个盘点单里面都有该资产编号的情况）
    function find(rid){
        $.ajax({
            url:"/findCheck",
            type:"post",
            dataType:"text",
            data:{
                "rid":rid
            },
            success:function(msg){
                //为ERROR表示有符合的资产表，但是扫描到的资产不在表里面。
                if (msg=="ERROR"){
                    $.ajax({
                        url:"/selectResourceById",
                        type:"post",
                        dataType:"json",
                        data:{
                            "rid":rid
                        },
                        success:function (resource){
                            showResource(rid)
                            document.getElementById("rid_").innerHTML=rid;
                            $("#checknotfind").dialog();
                            //alert("否"+arrayObj)
                        },
                        error:function (msg){
                            alert("该二维码或条形码无法对应上任何一个已有资产！\n请检查是否扫描正确的码!")
                        }
                    })
                }else{
                    //返回TRUE_ERROR表示没有盘点单，返回checkid表示有这个单号。
                    if (msg=="TRUE_ERROR"){
                        alert("当前全部盘点表已过期，请重新发起新的盘点表!")
                    }else {
                        //获取扫描到的、在盘点时间内的行项目
                        $.ajax({
                            url: "/getCheck",
                            type: "post",
                            dataType: "json",
                            data: {
                                "rid": rid,
                                "msg": msg
                            },
                            success: function (check) {
                                //展示到dialog
                                showData(check)
                                $("#checkresult").dialog();
                                alert("是" + arrayObj)
                            }
                        })
                    }
                }
            },
            error:function (msg){
                alert("数据库搜索盘点数据失败!\n请检查是否有未过期的盘点单"+msg);
            }
        })
    }

    //当盘点单里面没有resource的时候采用下面的方法来展示dialog页面
    function showResource(Resource){
        document.getElementById("rid_").innerHTML=Resource.rnum;
    }

    //把数据展示到dialog的页面
    function showData(check){
        document.getElementById("checkid").innerHTML=check.checkid;
        document.getElementById("rid").innerHTML=check.true_id;
        //document.getElementById("uid").innerHTML=check.initial_uid;
        document.getElementById("user").value=check.uname;
    }

    //目前这个函数没有用到了
    function sendMessage(message){
        $.ajax({
            url:"/managerCheck",
            type:"post",
            dataType:"text",
            data:{
                "message":message
            },
            success:function(msg){
                alert(msg);
            },
            error:function (msg){
                alert("ajax失败:"+msg);
            }
        })
    }
</script>
<body>

<div id="header">
        <h1>资产盘点系统</h1>
    </div>

    <div id="nav">
        <a href="manager/resource.html" target="frameBox" >资产管理</a>
        <div style="border:1px solid #acacac"></div>
        <a href="manager/user.html" target="frameBox">员工查看</a>
        <div style="border:1px solid #acacac"></div>
        <a href="manager/startcheck.html" target="frameBox">盘点管理</a>
        <div style="border:1px solid #acacac"></div>
        <a href="manager/manager.html" target="frameBox">权限管理</a>
<!--        <div style="border:1px solid #acacac"></div>-->
<!--        <a href="manager/test.html" target="right">测试下拉框</a>-->
        <div style="border:1px solid #acacac"></div>
        <button type="button" id="check" name="check">扫码盘点</button>
    </div>

    <div id="checkresult" title="盘点结果" >
        <form autocomplete="on">
            <label for="user">资产编号:</label>
            <a id="checkid">null</a>
            <br><label for="user">资产编号:</label>
            <a id="rid">null</a>
<!--            <br><label for="user">用户工号:</label>-->
<!--            <a id="uid"></a>-->
            <br><label>用户姓名</label>
            <input id="user" style="z-index:99999;position: relative">
            <br><input type="checkbox" checked='checked'>是否一致</input>
            <br><button type="button" id="submit">提交</button>
        </form>
    </div>

    <div id="checknotfind" title="未录入盘点单的资产" >
        <table>
            <tr><td>盘点不一致</td></tr>
            <tr><td> 资产编号</td></tr>
            <tr><td><a id="rid_">null</a></td></tr>
            <tr><td> 姓名</td></tr>
            <tr><td><input id="user_"></td></tr>
            <tr><td><button type="button" id="submit_not_true">提交</button> </td></tr>
        </table>
<!--        <form>-->
<!--            <div >盘点不一致</div>-->
<!--            <br><label for="user">资产编号:</label>-->
<!--            <a id="rid_">null</a>-->
<!--            <br><label>用户姓名</label>-->
<!--            <input id="user_" >-->
<!--            <br><button type="button" id="submit_not_true">提交</button>-->
<!--            <button type="button" id="search_user2">查找用户</button>-->
<!--        </form>-->
    </div>


    <div id="section">
        <iframe name="frameBox" width="100%" height="100%" src="manager/resource.html" frameborder="0" seamless></iframe>
    </div>
</body>
</html>